package com.itheima.demo;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import sun.applet.Main;
// import org.junit.Test;
// import pojo.Brand;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors;

/**
 * @author itheima
 * @since 2022-09-11
 */
public class JdbcClassDemo {
    public static void main(String[] args) throws Exception {
        // testSelectAll();
        // testAdd();
        testUpdate();
    }

    /**
     * 查询所有
     * 1. SQL：select * from tb_brand;
     * 2. 参数：不需要
     * 3. 结果：List<Brand>
     */
    // @Test
    public static void testSelectAll() throws Exception {

        // 完善代码

        // 1. 获取Connection
        // 3. 加载配置文件
        Properties properties = new Properties();
        properties.load(new FileReader("D:\\develop\\dm\\JDBCHomeWork\\src\\druid.properties"));
        // 4. 获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        // 5. 获取数据库连接 Connection
        Connection connection = dataSource.getConnection();
        // 3. 获取pstmt对象
        Statement statement = connection.createStatement();
        // 2. 定义SQL
        String sql = "select * from tb_brand";

        // 5. 执行SQL
        ResultSet rs = statement.executeQuery(sql);
        // 6. 处理结果 List<Brand> 封装Brand对象，装载List集合
        List<Brand> brands = new ArrayList<>();
        // 获取数据
        while (rs.next()) {
            int id = rs.getInt(1);
            String brandName = rs.getString(2);
            String companyName = rs.getString(3);
            int ordered = rs.getInt(4);
            String description = rs.getString(5);
            int status = rs.getInt(6);

            Brand brand = new Brand();
            brand.setId(id);
            brand.getBrandName(brandName);
            brand.getCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);
            // 装载集合
            brands.add(brand);

        }
        // 遍历集合
        for (Brand b : brands) {
            System.out.println(b);
        }


        // 7. 释放资源
        rs.close();
        statement.close();
        connection.close();

    }

    /**
     * 添加
     * 1. SQL：insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);
     * 2. 参数：需要，除了id之外的所有参数信息
     * 3. 结果：boolean
     */
    // @Test
    public static void testAdd() throws Exception {
        // 完善代码

        // 接收页面提交的参数

        // 1. 获取Connection
        // 3. 加载配置文件
        Properties properties = new Properties();
        properties.load(new FileReader("D:\\develop\\dm\\JDBCHomeWork\\src\\druid.properties"));
        // 4. 获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        // 5. 获取数据库连接 Connection
        Connection connection = dataSource.getConnection();
        // 3.获取pstmt对象
        Statement statement = connection.createStatement();

        // 2. 定义SQL
        String sql = "INSERT INTO tb_brand SET brand_name = '苹果', company_name = '苹果有限公司',ordered='60',description = '贵', status = '0'";


        // 5. 执行SQL
        int resultSet = statement.executeUpdate(sql);
        System.out.println(resultSet);

        // 7. 释放资源
        statement.close();
        connection.close();
    }

    /**
     * 修改
     * 1. SQL：
     * <p>
     * update tb_brand
     * set brand_name  = ?,
     * company_name= ?,
     * ordered     = ?,
     * description = ?,
     * status      = ?
     * where id = ?
     * <p>
     * 2. 参数：需要，所有数据
     * 3. 结果：boolean
     */
    // @Test
    public static void  testUpdate() throws Exception {
        // 完善代码
        Properties properties = new Properties();
        properties.load(new FileReader("D:\\develop\\dm\\JDBCHomeWork\\src\\druid.properties"));
        // 4. 获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        // 5. 获取数据库连接 Connection
        Connection connection = dataSource.getConnection();
        // 3.获取对象
        Statement statement = connection.createStatement();

        // 2. 定义SQL
        String sql = "UPDATE tb_brand SET ordered=5000 WHERE id=4 ;";

        int result = statement.executeUpdate(sql);
        System.out.println(result);

    //    关闭流
        statement.close();
        connection.close();

    }

    /**
     * 删除
     * 1. SQL：
     * delete from tb_brand where id = ?
     * 2. 参数：需要，id
     * 3. 结果：boolean
     */
    // @Test
    public void testDeleteById() throws Exception {
        // 完善代码
        String sql = "DELETE FROM tb_brand WHERE id=1;";
    }
}
